import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudentDialog extends JDialog {
    private JTextField idField;
    private JTextField nameField;
    private JTextField classField;
    private JTextField ageField;
    private JTextField heightField;
    private JPasswordField passwordField;
    private Student student;

    public StudentDialog(JFrame parent, String title, Student student) {
        super(parent, title, true);
        this.student = student;

        JPanel panel = new JPanel();
        panel.setLayout(null);

        JLabel idLabel = new JLabel("学号:");
        idLabel.setBounds(10, 10, 80, 25);
        panel.add(idLabel);

        idField = new JTextField(20);
        idField.setBounds(100, 10, 160, 25);
        panel.add(idField);

        JLabel nameLabel = new JLabel("姓名:");
        nameLabel.setBounds(10, 40, 80, 25);
        panel.add(nameLabel);

        nameField = new JTextField(20);
        nameField.setBounds(100, 40, 160, 25);
        panel.add(nameField);

        JLabel classLabel = new JLabel("班级:");
        classLabel.setBounds(10, 70, 80, 25);
        panel.add(classLabel);

        classField = new JTextField(20);
        classField.setBounds(100, 70, 160, 25);
        panel.add(classField);

        JLabel ageLabel = new JLabel("年龄:");
        ageLabel.setBounds(10, 100, 80, 25);
        panel.add(ageLabel);

        ageField = new JTextField(20);
        ageField.setBounds(100, 100, 160, 25);
        panel.add(ageField);

        JLabel heightLabel = new JLabel("身高:");
        heightLabel.setBounds(10, 130, 80, 25);
        panel.add(heightLabel);

        heightField = new JTextField(20);
        heightField.setBounds(100, 130, 160, 25);
        panel.add(heightField);

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(10, 160, 80, 25);
        panel.add(passwordLabel);

        passwordField = new JPasswordField(20);
        passwordField.setBounds(100, 160, 160, 25);
        panel.add(passwordField);

        JButton saveButton = new JButton("保存");
        saveButton.setBounds(10, 190, 80, 25);
        panel.add(saveButton);

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveStudent();
            }
        });

        if (student != null) {
            idField.setText(student.getId());
            nameField.setText(student.getName());
            classField.setText(student.getClassName());
            ageField.setText(String.valueOf(student.getAge()));
            heightField.setText(String.valueOf(student.getHeight()));
            passwordField.setText(student.getPassword());
        }

        add(panel);
        setSize(300, 250);
        setLocationRelativeTo(parent);
    }

    private void saveStudent() {
        if (student == null) {
            student = new Student();
        }

        student.setId(idField.getText());
        student.setName(nameField.getText());
        student.setClassName(classField.getText());
        student.setAge(Integer.parseInt(ageField.getText()));
        student.setHeight(Double.parseDouble(heightField.getText()));
        student.setPassword(new String(passwordField.getPassword()));

        if (getTitle().equals("添加学生")) {
            ((StudentManagementPanel) getParent()).addStudent(student);
        } else {
            ((StudentManagementPanel) getParent()).updateStudent(student);
        }

        dispose();
    }
}

